<template>
	<view class="container-login">
		<view class="wechatapp">
			<view class="header">
				<open-data class="" type="userAvatarUrl"></open-data>
			</view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
		<view class="login-btn">
			<button class="btn-normal" open-type="getPhoneNumber" lang="zh_CN" @getphonenumber="handleLogin">授权登录</button>
		</view>
		<view class="no-login-btn">
			<button class="btn-normal" @click="onNotLogin">暂不登录</button>
		</view>
	</view>
</template>
<script>
	import {
		UserApi
	} from '@/api'
	import {
		toPromise
	} from '@/utils/util'
	export default {
		data() {
			return {
				// recommend_id: app.globalData.recommend_id, // 推荐人id
			}
		},
		onLoad(options) {},
		methods: {
			async handleLogin(e) {
				try {
					this.$showLoading()
					const uniLogin = await toPromise(uni.login)
					const openIdData = await UserApi.MiniCodeLogin({
						code: uniLogin.code
					})
					const phoneData = await UserApi.getUserMobile({
						openid: openIdData.result.openid,
						iv: e.detail.iv,
						encryptedData: e.detail.encryptedData
					})
					let loginForm = {
						openid: openIdData.result.openid,
						mobile: phoneData.data.phoneNumber,
					}
					// 邀请
					const inviteCode = uni.getStorageSync('inviteCode')
					if (inviteCode) loginForm.recommend_id = inviteCode
					// 登录
					const loginInfo = await UserApi.login(loginForm)
					this.$store.commit('user/updateToken', loginInfo.result.openid)
					this.$store.commit('user/updateUserInfo', loginInfo.result)
					// 清除邀请码
					uni.removeStorageSync('inviteCode')
					uni.hideLoading()
					this.$showToast('登录成功')
					uni.navigateBack()
				} catch (err) {
					console.log(err);
					uni.hideLoading()
					if (err.message === '-40012: Iv 非法')
						return
					this.$showToast(err || err?.message)
				}
			},
			onNotLogin() {
				return uni.navigateBack()
			}
		},
	}
</script>
<style>
	page {
		background: #fff;
		font-size: 32rpx;
	}
</style>
<style lang="scss" scoped>
	.container-login {
		padding: 0 60rpx;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;
	}

	.wechatapp .header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: linear-gradient(173.2deg, #f8b280 0%, #ff8b38 100%);
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}

	.no-login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #fff;
		color: #FF890B;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
		border: 2rpx solid #FD8B07;
	}
</style>